﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.CMS;
using PowerCreator.LMS.CMS.Entity;
using Telerik.WebControls;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_NewManager_AddNewType : PowerCreator.LMS.Web.Base.BasePage
{
    NewType objNewType = new NewType();
    NewTemplet objNewTemplet = new NewTemplet();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            int NewTypeID = 0; 
            if (Request.QueryString["NewTypeID"] != null)
            {
                NewTypeID = int.Parse(Request.QueryString["NewTypeID"].ToString());
                ViewState["NewTypeID"] = NewTypeID.ToString();
                BinderForm(NewTypeID);
            }

            BinderNewTemplet(NewTypeID);
        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 3)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }


    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderForm(int id)
    {
        VOTNewType vo = new VOTNewType();
        vo = objNewType.Load(id);
        txtNewTypeName.Text = vo.NewTypeName;
        txt_pic.Text = vo.NewTypePicUrl;
        if (vo.IsTop)
        {
            rdbtn_topyes.Checked = true;
            rdbtn_topno.Checked = false;
        }
        else
        {
            rdbtn_topyes.Checked = false;
            rdbtn_topno.Checked = true;
        }

        if (vo.IsShow)
        {
            rdbtn_showpyes.Checked = true;
            rdbtn_showpno.Checked = false;
        }
        else
        {
            rdbtn_showpyes.Checked = false;
            rdbtn_showpno.Checked = true;
        }

        if (vo.FKNewTemlletID != null)
        {
            if (vo.FKNewTemlletID > 0)
            {
                rdbtn_Yes.Checked = true;
                tr_NewTemplet.Visible = true;
            }
        }
    }
    private void BinderNewTemplet(int typeid)
    {
        string search="";
        DataTable dt = objNewTemplet.Query(search);
        rptNewTemplet.DataSource = dt;
        rptNewTemplet.DataBind();

        VOTNewType vo = new VOTNewType();
        vo = objNewType.Load(typeid);

        foreach (RepeaterItem item in rptNewTemplet.Items)
        {
            Label lbl_TempletID = (Label)item.FindControl("lbl_TempletID");
            if (lbl_TempletID.Text == vo.FKNewTemlletID.ToString())
            {
                Label lbl_IsSel = (Label)item.FindControl("lbl_IsSel");
                lbl_IsSel.Text = "&nbsp;&nbsp;<font color=green>选中</font>";
            }
        }

    }


    protected void rdbtn_Yes_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbtn_Yes.Checked)
        {
            tr_NewTemplet.Visible = true;
        }
        else if (rdbtn_No.Checked)
        {
            tr_NewTemplet.Visible = false;
            lbl_NewTempletNewID.Text = "0";
        }
    }
    protected void rdbtn_No_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbtn_Yes.Checked)
        {
            tr_NewTemplet.Visible = true;
        }
        else if (rdbtn_No.Checked)
        {
            tr_NewTemplet.Visible = false;
            lbl_NewTempletNewID.Text = "0";
        }
    }

    private bool CheckPageData()
    {
        string search = string.Format("NewTypeName = '{0}'", txtNewTypeName.Text);
        if (ViewState["NewTypeID"] != null)
        {
            search += "and NewTypeID<>" + ViewState["NewTypeID"].ToString();
        }
        if (objNewType.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTNewType voNewType = new VOTNewType();

        if (ViewState["NewTypeID"] == null)
        {
            voNewType.NewTypeName = txtNewTypeName.Text.Trim();

            voNewType.NewTypePicUrl = txt_pic.Text;
            if (rdbtn_topno.Checked)
                voNewType.IsTop = false;
            else
                voNewType.IsTop = true;

            if (rdbtn_showpno.Checked)
                voNewType.IsShow = false;
            else
                voNewType.IsShow = true;

            if (rdbtn_Yes.Checked)
                voNewType.FKNewTemlletID = int.Parse(lbl_NewTempletNewID.Text);
            else if (rdbtn_No.Checked)
                voNewType.FKNewTemlletID = 0;
            if (objNewType.AddNewType(voNewType))
            {
                string url = "ManagerNewType.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            voNewType = objNewType.Load(int.Parse(ViewState["NewTypeID"].ToString()));
            voNewType.NewTypeName = txtNewTypeName.Text.Trim();
            voNewType.NewTypePicUrl = txt_pic.Text;
            if (rdbtn_topno.Checked)
                voNewType.IsTop = false;
            else
                voNewType.IsTop = true;

            if (rdbtn_showpno.Checked)
                voNewType.IsShow = false;
            else
                voNewType.IsShow = true;

            if (rdbtn_Yes.Checked)
                voNewType.FKNewTemlletID = int.Parse(lbl_NewTempletNewID.Text);
            else if (rdbtn_No.Checked)
                voNewType.FKNewTemlletID = 0;

            //if (rbt_del_yes.Checked)
            //{
            //    string sql = "update TNew set NewFileUrl=NULL where FKNewNewTypeID=" + voNewType.NewTypeID.ToString();
            //    PowerCreator.LMS.ViewQuery.ViewQuery vo = new PowerCreator.LMS.ViewQuery.ViewQuery();
            //    vo.u
            //}
            if (objNewType.Update(voNewType))
            {
                string url = "ManagerNewType.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManagerNewType.aspx";
        Script.RedirectURL(url);
    }


    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Label lbl_IsSel = (Label)e.Item.FindControl("lbl_IsSel");
                lbl_IsSel.Text = "";
            }
        }
    }

    protected void rptNewTemplet_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "leditor")
        {
            foreach (RepeaterItem item  in rptNewTemplet.Items)
            {
                Label lbl_IsSel_li = (Label)item.FindControl("lbl_IsSel");
                lbl_IsSel_li.Text = "";
            }
            Label lbl_IsSel = (Label)e.Item.FindControl("lbl_IsSel");
            lbl_NewTempletNewID.Text = e.CommandArgument.ToString();
            lbl_IsSel.Text = "&nbsp;&nbsp;<font color=green>选中</font>";

        }
        else if (e.CommandName.ToLower() == "islook")
        {
            if (System.IO.File.Exists(e.CommandArgument.ToString()))
            {
                Script.RedirectURL(e.CommandArgument.ToString());
            }
            else
            {
                Script.Alert("不存在此模板，可能该文件已经被删除！");
            }
            
        }

    }


    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //btn_Back.Enabled = false;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Url = "";
            Url = WebUtils.WebRoot + "/fileManager/UploadFile/NewTypeFile/";
            string Path = Server.MapPath(Url);

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            string filetype = file.GetExtension();
            if (!IsUploadFileType(filetype)) return;

            //组合路径，file.GetName()取得文件名
            string fileName = opCourse.GetUploadResourceFileName(file.GetExtension());

            Path = Path + fileName;
            Url = Url + fileName;
            //保存
            file.SaveAs(Path, true);
            txt_pic.Text = Url;
            btn_Back.Enabled = true;
        }
    }

    //检查上传文件类型
    public bool IsUploadFileType(string extension)
    {
        string allowExtensions = "gif|jpg";    //允许上传的类型
        if (!opCourse.IsAllowedExtension(extension, allowExtensions))
        {
            //lbl_FileUpload_msg_FileType.Text = "注意："+GetLanageValueByKey("AddCourseInfo_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + allowExtensions;
            return false;
        }
        return true;
    }
}
